package io.bdmc.modules.bss.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceMachineDtsConfig;
import io.bdmc.modules.bss.service.IDeviceDeviceService;
import io.bdmc.modules.bss.service.IDeviceMachineDtsConfigService;
import io.bdmc.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ping
 * @since 2021-11-19
 */
@RestController
@RequestMapping("/bss/dts/config")
public class DeviceMachineDtsConfigController extends AbstractController {

    @Autowired
    IDeviceDeviceService deviceService;


    @Autowired
    IDeviceMachineDtsConfigService dtsConfigService;



    @GetMapping("/list")
    public R list(int deviceId) {
        final List<DeviceMachineDtsConfig> list = dtsConfigService.list(new QueryWrapper<DeviceMachineDtsConfig>().eq("device_id", deviceId));
        return R.ok().putData(list);
    }

    @GetMapping("/info")
    public R info(int deviceId,int channel) {
        final DeviceMachineDtsConfig dtsConfig = dtsConfigService.getOneByDeviceIdAndChannel(deviceId,channel);
        return R.ok().putData(dtsConfig);
    }

    @SysLogger("新增dts告警配置")
    @PostMapping("/add")
    public R add(@RequestBody DeviceMachineDtsConfig dtsConfig) {
        long count = dtsConfigService.countByDeviceIdAndChannel(dtsConfig.getDeviceId(),dtsConfig.getChannel());
        if(count>0){
            return R.error(-1,"端口已存在配置，无法重复添加");
        }
        dtsConfig.setCreateTime(LocalDateTime.now());
        Boolean b = dtsConfig.insert();
        return R.trueOrFalse(b);
    }

    @SysLogger("修改dts告警配置")
    @PostMapping("/update")
    public R update(@RequestBody DeviceMachineDtsConfig dtsConfig) {
        Boolean b = dtsConfig.updateById();
        return R.trueOrFalse(b);
    }

    @SysLogger("删除dts告警配置")
    @DeleteMapping("/delete/{dtsConfigId}")
    public R delete(@PathVariable Integer dtsConfigId) {
        if (dtsConfigId > 0)
            dtsConfigService.removeById(dtsConfigId);
        return R.ok();
    }

}

